
#找出文件中所有访问记录里。访问次数最多的前10个IP，访问最频繁的前10个页面，并使用pyechart画出饼状图
# 统计ip 及其次数 ，url 及其次数。类型转换。--不用
# 统计 画图 ，结果存储
import re
s = '60.191.66.222 - - [18/Nov/2019:03:46:17 +0800] "GET /manager/html HTTP/1.1" 502 575 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)" "-"'
pattern = r'(?P<remote>[0-9.]{7,}) - - (?P<datetime>\[[\w/:]+ .*]) \"(?P<method>[A-Z]{3,}) (?P<url>.*) HTTP.*" (?P<stats>\d+) (?P<length>\d+) .*" (?P<useragent>.*") ".*'
from  collections import Counter
def serch(pattern,log_path,allow:str):
    # 这个allow 是不是没有用上啊？
    url = []
    remote = []
    with   open(log_path, 'br') as f:
        ts = f.readlines()
        for i in ts:
            [url.append(i.group('url')) for i in re.compile(pattern).finditer(i.decode(encoding='utf8')) if i]
            [remote.append(i.group('remote')) for i in re.compile(pattern).finditer(i.decode(encoding='utf8')) if i ]
            # 还可以把 提取何种数据 单独为装饰器。
    a = Counter(url).most_common(10)
    b = Counter(remote).most_common(10)
    return a,b

url,ip = serch(pattern,'access.log-20191119','remote url')
from pyecharts.charts import Pie
# pie = Pie()
# pie.add(
#     "",
#     ip,
# )
# pie.render(path="ip.html")
# pie = Pie()
# pie.add(
#     "",
#     url,
# )
# pie.render(path="url.html")

def render(data,title = None):
    if not title:
        title = 'title'
    pie = Pie()
    pie.add(
        '',
        data,
    )
    pie.render(path="{}.html".format(title))
render(ip,'ip')
render(url,'url')

# 晚上没有找到最新版本的 帮助，目前只做了最基础的展示···